/*
 * @(#)Tagging.java 0.0.1-SNAPSHOT
 * Copyright Group B
 */

package grupb.upf.XMLObjects.impl;

import grupb.upf.XMLObjects.Tag;
import java.io.Serializable;
import java.util.List;
import java.util.Vector;

/**
 * <p>This class implements the inferface tagging. It is used to manage
 * personal and predefined tags of a new.</p>
 *
 * @version    0.0.1-SNAPSHOT
 * @author     Group B
 */
public final class Tagging implements grupb.upf.XMLObjects.Tagging,
        Serializable {

    /**
     * The serialVersionUID is created to be able to serialize this class, it's
     * necessary because it will be sent.
     */
    private static final long serialVersionUID = 1L;
    /**
     * This vector contains all the predefined tags related to the object.
     */
    private List predefinedTags = new Vector();
    /**
     * This vector contains all the user tags related to the object.
     */
    private List personalTags = new Vector();
    /**
     * The separator between tags.
     */
    private static final String SEPARATOR = ", ";
    /**
     * This method allows us to set the predefined tag list.
     * @param setPredefinedTags the list of predefined tags
     */
    @Override
    public void setPredefinedTags(final List setPredefinedTags) {
        this.predefinedTags = setPredefinedTags;
    }
    /**
    * This method allow us to add the predefined tag list.
    * @param predefinedTag is the predefined tag
    */
    @Override
    public void addPredefinedTag(final Tag predefinedTag) {
        this.predefinedTags.add(predefinedTag);
    }
    /**
     * This metho allows us to remove a predefined tag.
     * @param predefinedTag The predefined tag.
     */
    @Override
    public void removePredefinedTag(final Tag predefinedTag) {
        this.predefinedTags.remove(predefinedTag);
    }
    /**
     * This method allow us to get the predefined tag list.
     * @return the predefined tag list
     */
    @Override
    public List getPredefinedTags() {
        return this.predefinedTags;
    }
    /**
     * This method allows us to empty the vector with the predefined tags.
     */
    @Override
    public void clearPredefinedTags() {
        this.predefinedTags.clear();
    }
    /**
     * This method allow us to set the personal tags list.
     * @param setPersonalTags the personal tag list
     */
    @Override
    public void setPersonalTags(final List setPersonalTags) {
        this.personalTags = setPersonalTags;
    }
    /**
     * This method allows us to add a personal tag.
     * @param personalTag the personal tag
     */
    @Override
    public void addPersonalTag(final Tag personalTag) {
        this.personalTags.add(personalTag);
    }
    /**
     * This method allow us to remove a personal tag.
     * @param personalTag the personal tag to remove
     */
    @Override
    public void removePersonalTag(final Tag personalTag) {
        this.personalTags.remove(personalTag);
    }
    /**
     * This method allow us to get the personal tags list.
     * @return the personal tags list
     */
    @Override
    public List getPersonalTags() {
        return this.personalTags;
    }
    /**
     * This method allow us to get the tags as an String.
     * @return the tags as an String
     */
    @Override
    public String getTagsAsString() {
        StringBuffer sb = new StringBuffer();
        sb.append(getPersonalTagsAsString());
        sb.append(SEPARATOR);
        sb.append(getPredefinedTagsAsString());
        if (sb.toString().equals(SEPARATOR)) {
            return "";
        }
        if (sb.length() > 0 && sb.substring(
                sb.length() - SEPARATOR.length())
                .equalsIgnoreCase(SEPARATOR)) {
            return sb.substring(0, sb.length() - SEPARATOR.length());
        }
        return sb.toString();
    }
    /**
     * This method allow us to get the personal tags as an String.
     * @return the personal tags as an String
     */
    private String getPersonalTagsAsString() {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < personalTags.size(); i++) {
            sb.append(((PersonalTag) personalTags.get(i)).getTag());
            sb.append(SEPARATOR);
        }
        if (sb.length() > 0) {
            return sb.substring(0, sb.length() - SEPARATOR.length());
        }
        return "";
    }

    /**
     * This method allow us to get the predefined tags as an String.
     * @return the predefined tags as an String
     */
    private String getPredefinedTagsAsString() {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < predefinedTags.size(); i++) {
            sb.append(((PredefinedTag) predefinedTags.get(i)).getTag());
            sb.append(SEPARATOR);
        }
        if (sb.length() > 0) {
            return sb.substring(0, sb.length() - SEPARATOR.length());
        }
        return "";
    }
    /**
     * This method allows us to delete the personal tag list.
     */
    @Override
    public void clearPersonalTags() {
        this.personalTags.clear();
    }

}
